<template>
  <div class="main">
    <!-- 头部组件 -->
    <comp-det-heater>
      <template v-slot:left>
        <div class="left" @click="cancel"><van-icon name="arrow-left" /></div>
      </template>
      <template v-slot:center>
        <span>发布笔记</span>
      </template>
    </comp-det-heater>
    <!-- 内容编辑 -->
    <div class="edit">
      <div class="photo">
        <!-- <img :src="imgs" alt="" />
        <input
          accept="image/*,audio/*"
          type="file"
          class="file"
          @change="getFiles($event)"
        /> -->
        <van-uploader
          v-model="fileList"
          multiple
          accept="image/*"
          :max-count="8"
        />
      </div>
      <div class="title">
        <el-input
          placeholder="填写标题会有更多赞哦~"
          v-model="title"
          clearable
          class="titleInput"
        >
        </el-input>
      </div>
      <div class="content">
        <el-input
          type="textarea"
          :rows="5"
          placeholder="添加正文"
          v-model="content"
        >
        </el-input>
      </div>
      <div class="model">
        <el-input placeholder="推荐色号" v-model="model" class="modelInput">
        </el-input>
      </div>
      <ul>
        <li @click="topic">#话题</li>
        <li>@用户</li>
      </ul>
      <div id="container">

      </div>
    </div>
    
    <!-- 发布栏 -->
    <div class="bottom">
      <div class="btn" @click="over">发布笔记</div>
    </div>
    <!-- 提示框 -->
    <div class="propupBox" v-show="isShow">
      <div class="propup">
        <span>是否退出发布</span>
        <div class="btn">
          <button @click="returnFound">确定</button>
          <button @click="cancel">取消</button>
        </div>
      </div>
    </div>
  </div>
</template>


<script>
import CompDetHeater from "../components/comp-detHeater.vue";
import { mapState } from "vuex";
export default {
  data() {
    return {
      title: "",
      model: "",
      content: "",
      // imgs: require("../assets/img/add.jpg"),
      isShow: false,
      fileList: [],
      fileData: [],
    };
  },
  
  methods: {
    // 获取图片地址
    getFiles() {
      console.log(this.fileList);
      for (let i = 0; i < this.fileList.length; i++) {
        this.fileData.push(this.fileList[i].content);
      }
    },
    returnFound() {
      this.$router.push({
        path: "/home/found",
      });
    },
    cancel() {
      this.isShow = !this.isShow;
    },
    topic(){
      this.content = this.content + '#'
    },
    // 发布笔记
    over() {
      if (
        this.title.length == 0 ||
        this.model.length == 0 ||
        this.content.length == 0
      ) {
        alert("输入内容不能为空");
      } else {
        this.getFiles();
        let index = 1
        for (let i = 0; i < this.users.length; i++) {
          index++;
        }
        let obj = {
          detailsImg: [],
        };
        // 将信息存储到对象中
        obj.id = index;
        obj.name = this.mine.name;
        obj.head = this.mine.url;
        obj.url = this.fileData[0];
        obj.content = this.title;
        obj.fabulous = 0;
        obj.collection = 0;
        obj.title = this.title;
        obj.details = this.content;
        obj.model = this.model;
        obj.detailsImg = this.fileData;
        obj.isCollect = false;
        obj.isLike = false;
        obj.comment = []
        this.users.push(obj);
        this.myNote.push(obj);
        this.$router.push({
          path: "/home/found",
        });
      }
      console.log(this.users);
    },
    
  },
  computed: {
    ...mapState(["mine",'users','myNote']),
  },
  components: {
    CompDetHeater,
  },
};
</script>
<style scoped lang="scss">
.main {
  margin-top: 60px;
  background: #fafafa;
  .edit {
    width: 100%;
    padding: 10px;
    box-sizing: border-box;
    font-size: 20px;
    .photo {
      width: 100%;
      padding: 10px 0;
      box-sizing: border-box;
    }
    .title {
      width: 100%;
      height: 50px;
      border-bottom: 1px solid #ccc;
    }
    .content {
      margin-top: 10px;
      box-sizing: border-box;
      width: 100%;
    }
    .model {
      border-bottom: 1px solid #ccc;
      width: 100%;
      height: 40px;
      padding: 10px 0;
      .modelInput {
        float: right;
        width: 100px;
      }
    }
    ul {
      display: flex;
      padding: 10px 0;
      li {
        background: #ccc;
        border-radius: 5px;
        margin-right: 10px;
        font-size: 14px;
        color: #000;
        padding: 5px 10px;
      }
    }
    .address{
      font-size: 14px;
    }
    #container {
      width:300px;
      height: 180px; } 
  }
  
  .bottom {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 80px;
    background: #fafafa;
    border-top: 1px solid #ccc;
    .btn {
      margin: 15px auto;
      width: 80%;
      height: 50px;
      background: #e93f48;
      border-radius: 20px;
      font-size: 20px;
      color: #fff;
      text-align: center;
      line-height: 50px;
    }
  }
  .propupBox {
    z-index: 999999999;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.2);
    .propup {
      width: 270px;
      height: 170px;
      background: #fff;
      margin: 0 auto;
      margin-top: 300px;
      border-radius: 20px;
      span {
        display: block;
        width: 100%;
        height: 110px;
        line-height: 110px;
        font-size: 22px;
        text-align: center;
        color: rgb(104, 104, 104);
      }
      .btn {
        width: 100%;
        height: 50px;
        display: flex;
        justify-content: space-around;
        button {
          font-size: 12px;
          height: 35px;
          width: 50px;
          border-radius: 5px;
          border: 1px solid rgb(156, 156, 156);
        }
      }
    }
  }
}
</style>